﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace OWINServer
{
    /// <summary>
    /// 实现被外部调用的接口。当收到信息后触发OWINTriggerEvent事件，将消息传给桌面前端应用
    /// </summary>
    public class IncomingApiController : ApiController
    {
        [HttpGet]
        public IEnumerable<string> TestGet()
        {
            return new string[] { "Success" };
        }

        /// <summary>
        /// 电话呼入接口
        /// api/<controller>
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost]
        public string IncomingCall([FromBody] object value)
        {
            string recvStr = value?.ToString();
            if (string.IsNullOrEmpty(recvStr))
            {
                var request = HttpContext.Current?.Request;
                if (request != null)
                    recvStr = request.Params.Get("data");

                if (string.IsNullOrEmpty(recvStr))
                {
                    ResponseMsg failedMsg = new ResponseMsg()
                    {
                        code = -1,
                        msg = "失败",
                        data = { }
                    };

                    return JsonConvert.SerializeObject(failedMsg);
                }
            }

            // 将接收到的信息赋给事件类中Message属性
            OWINStart.TriggerEvent.Message = recvStr;
            // 触发事件类中事件，将消息传递给桌面程序绑定的方法
            OWINStart.TriggerEvent.OnMessage();

            ResponseMsg msg = new ResponseMsg()
            {
                code = 200,
                msg = "成功",
                data = { }
            };

            return JsonConvert.SerializeObject(msg);
        }

        /// <summary>
        /// 通话状态推送接口
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost]
        public string CommunicateStatus([FromBody] object value)
        {
            string recvStr = value?.ToString();
            if (string.IsNullOrEmpty(recvStr))
            {
                var request = HttpContext.Current?.Request;
                if (request != null)
                    recvStr = request.Params.Get("data");

                if (string.IsNullOrEmpty(recvStr))
                {
                    ResponseMsg failedMsg = new ResponseMsg()
                    {
                        code = -1,
                        msg = "失败",
                        data = { }
                    };

                    return JsonConvert.SerializeObject(failedMsg);
                }
            }

            // 将接收到的信息赋给事件类中Message属性
            OWINStart.TriggerEvent.Message = recvStr;
            // 触发事件类中事件，将消息传递给桌面程序绑定的方法
            OWINStart.TriggerEvent.OnStatus();

            ResponseMsg msg = new ResponseMsg()
            {
                code = 200,
                msg = "成功",
                data = { }
            };

            return JsonConvert.SerializeObject(msg);
        }

        public string GetStreamStr(Stream stream)
        {
            try
            {
                using (var sr = new StreamReader(stream, Encoding.UTF8))
                {
                    var task = sr.ReadToEndAsync();
                    task.Wait();
                    return task.Result;
                }
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger(typeof(IncomingApiController)).Error("返回错误：" + ex.ToString());
            }

            return string.Empty;
        }
    }
}
